
#include "member_BMainWindow.h"
#include "member_BWidget.h"

using namespace BWE;

member_BMainWindow::member_BMainWindow(BMainWindow* mainWindow)
{
	boss = mainWindow;
	titleAlign = Align_Left | Align_VCenter;
	titleSpace = 5;
	titleHovered = false;
	dirty = true;
	closeHovered = false;
	minHovered = false;
	maxHovered = false;
	pressed = false;
}
member_BMainWindow::~member_BMainWindow()
{

}

void member_BMainWindow::freshRects()
{
	int w = boss->width();
	int h = boss->height();
	int b = boss->border();

	titleRect = boss->rect(Part_Title);
	borderLeft.set(0, b, b, h - b - b);
	borderTop.set(b, 0, w - b - b, b);
	borderRight.set(w - b, b, b, h - b - b);
	borderBottom.set(b, h - b, w - b - b, b);

	int left = boss->perchs().left();
	int right = boss->perchs().right();
	int top = boss->perchs().top();
	int bottom = boss->perchs().bottom();

	BSize realButtonSize = buttonSize;
	if (realButtonSize.width() == 0)
		realButtonSize.width() = top;
	if (realButtonSize.height() == 0)
		realButtonSize.height() = top;
	BRect closeArea(w - b - right - realButtonSize.width(), b, realButtonSize.width(), top);
	BRect maxArea = closeArea - BPoint(realButtonSize.width() + titleSpace, 0);
	BRect minArea = maxArea - BPoint(realButtonSize.width() + titleSpace, 0);
	closeRect = closeArea.align(Align_Center, realButtonSize);
	maxRect = maxArea.align(Align_Center, realButtonSize);
	minRect = minArea.align(Align_Center, realButtonSize);

	BRect iconArea(titleRect.x(), titleRect.y(), 0, titleRect.height());
	BSize realIconSize;
	if (titleIcon)
	{
		iconArea.setWidth(titleRect.height());
		realIconSize = iconSize;
		if (realIconSize.width() < 1)
			realIconSize.width() = titleRect.height();
		if (realIconSize.height() < 1)
			realIconSize.height() = titleRect.height();
		realIconSize.wrap(iconArea.size());
		iconRect = iconArea.align(Align_Center, realIconSize);
	}

	const BFont* font = boss->font();
	if (font && boss->title().size())
	{
		const BStyle* realStyle = boss->realStyle();
		if (realStyle)
			realStyle->trans(boss->title(), tr_title);
		else
			tr_title.clear();

		BSize textSize;
		if (tr_title.size())
			textSize = font->textSize(tr_title);
		else
			textSize = font->textSize(boss->title());
		BRect textArea = titleRect;
		textArea.left() = iconArea.right() + titleSpace;
		textArea.right() = minArea.left() - titleSpace;
		textRect = textArea.align(titleAlign, textSize);
	}
}

